﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.Visionox_Gamma
{
    public partial class RM692Ax_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();

        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;
        private double B_x = 0.14;
        private double B_y = 0.06;
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];

        private static bool Gamma_adjust_timeout_flag = false;

        private ushort[] gamma_r = new ushort[33];
        private ushort[] gamma_g = new ushort[33];
        private ushort[] gamma_b = new ushort[33];

        //Gamma绑点定义
        private byte[] Point_RM692Ax = new byte[33] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        //RM692Ax 监听挂载
        frmMain frmHelp = null;
        public RM692Ax_Gamma(frmMain RM692Ax)
        {
            frmHelp = RM692Ax;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体       
        }
        //RM692Ax 监听卸载
        private void RM692Ax_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM692Ax_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.Child_window_hide_already = false;
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Ax_Gamma_FormClosed！\r\n";
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void RM692Ax_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM692Ax_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Ax_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(RM692Ax_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Ax_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }

        //串口接收
        private void RM692Ax_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x3E: RM692Ax的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x3E && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }

                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        //---------------------------------------------------RM692AX---------------------------------------
                        if (Rec_tmp[0] == 0x3E)
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01: { if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n"; } break;
                                case 0x02:
                                    {
                                        int i;
                                        string tmp = "";
                                        for (i = 3; i <= (Rec_tmp[2] + 1); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                        }
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]);

                                        RM692AX_Reg_Data.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xf0) == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x40) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x80) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x07:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x06:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Gamma 绑点重置 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma1 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma1 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma2 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma2 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma3 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma3 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma4 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写GOA OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x10:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Power OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Power OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        string tmp = "";
                                        tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                        RM692AX_Time.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写第" + RM692AX_Time.Text + "次\t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写第" + RM692AX_Time.Text + "次\t写 OK！\r\n";
                                    }
                                    break;
                                case 0x20:
                                    {
                                        if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Change Gray\t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Change GrayP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x1C:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Others OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Others OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x04:
                                    {
                                        if (Setting.TempRecord.RM692AXCmdType == 0x03) //red
                                        {
                                            RM692AX_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            RM692AX_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            RM692AX_R2.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            RM692AX_R3.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            RM692AX_R4.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            RM692AX_R5.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            RM692AX_R6.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            RM692AX_R7.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            RM692AX_R8.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            RM692AX_R9.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            RM692AX_R10.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            RM692AX_R11.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            RM692AX_R12.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            RM692AX_R13.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            RM692AX_R14.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            RM692AX_R15.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            RM692AX_R16.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            RM692AX_R17.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            RM692AX_R18.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            RM692AX_R19.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            RM692AX_R20.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            RM692AX_R21.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            RM692AX_R22.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            RM692AX_R23.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            RM692AX_R24.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            RM692AX_R25.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            RM692AX_R26.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            RM692AX_R27.Value = Decimal.Parse((Rec_tmp[58] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                            RM692AX_R28.Value = Decimal.Parse((Rec_tmp[60] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                            RM692AX_R29.Value = Decimal.Parse((Rec_tmp[62] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                            RM692AX_R30.Value = Decimal.Parse((Rec_tmp[64] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                            RM692AX_R31.Value = Decimal.Parse((Rec_tmp[66] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                            RM692AX_R32.Value = Decimal.Parse((Rec_tmp[68] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM692AXCmdType == 0x04) //green
                                        {
                                            RM692AX_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            RM692AX_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            RM692AX_G2.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            RM692AX_G3.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            RM692AX_G4.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            RM692AX_G5.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            RM692AX_G6.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            RM692AX_G7.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            RM692AX_G8.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            RM692AX_G9.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            RM692AX_G10.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            RM692AX_G11.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            RM692AX_G12.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            RM692AX_G13.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            RM692AX_G14.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            RM692AX_G15.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            RM692AX_G16.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            RM692AX_G17.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            RM692AX_G18.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            RM692AX_G19.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            RM692AX_G20.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            RM692AX_G21.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            RM692AX_G22.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            RM692AX_G23.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            RM692AX_G24.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            RM692AX_G25.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            RM692AX_G26.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            RM692AX_G27.Value = Decimal.Parse((Rec_tmp[58] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                            RM692AX_G28.Value = Decimal.Parse((Rec_tmp[60] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                            RM692AX_G29.Value = Decimal.Parse((Rec_tmp[62] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                            RM692AX_G30.Value = Decimal.Parse((Rec_tmp[64] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                            RM692AX_G31.Value = Decimal.Parse((Rec_tmp[66] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                            RM692AX_G32.Value = Decimal.Parse((Rec_tmp[68] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM692AXCmdType == 0x05) //blue
                                        {
                                            RM692AX_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            RM692AX_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            RM692AX_B2.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            RM692AX_B3.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            RM692AX_B4.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            RM692AX_B5.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            RM692AX_B6.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            RM692AX_B7.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            RM692AX_B8.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            RM692AX_B9.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            RM692AX_B10.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            RM692AX_B11.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            RM692AX_B12.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            RM692AX_B13.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            RM692AX_B14.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            RM692AX_B15.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            RM692AX_B16.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            RM692AX_B17.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            RM692AX_B18.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            RM692AX_B19.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            RM692AX_B20.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            RM692AX_B21.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            RM692AX_B22.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            RM692AX_B23.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            RM692AX_B24.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            RM692AX_B25.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            RM692AX_B26.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            RM692AX_B27.Value = Decimal.Parse((Rec_tmp[58] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                            RM692AX_B28.Value = Decimal.Parse((Rec_tmp[60] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                            RM692AX_B29.Value = Decimal.Parse((Rec_tmp[62] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                            RM692AX_B30.Value = Decimal.Parse((Rec_tmp[64] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                            RM692AX_B31.Value = Decimal.Parse((Rec_tmp[66] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                            RM692AX_B32.Value = Decimal.Parse((Rec_tmp[68] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                default: break;
                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        //Gamma保存
        private void RM692Ax_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + RM692AX_R0.Text + "," + "G0," + RM692AX_G0.Text + "," + "B0," + RM692AX_B0.Text + "\r\n";
                    str = str + "R1," + RM692AX_R1.Text + "," + "G1," + RM692AX_G1.Text + "," + "B1," + RM692AX_B1.Text + "\r\n";
                    str = str + "R2," + RM692AX_R2.Text + "," + "G2," + RM692AX_G2.Text + "," + "B2," + RM692AX_B2.Text + "\r\n";
                    str = str + "R3," + RM692AX_R3.Text + "," + "G3," + RM692AX_G3.Text + "," + "B3," + RM692AX_B3.Text + "\r\n";
                    str = str + "R4," + RM692AX_R4.Text + "," + "G4," + RM692AX_G4.Text + "," + "B4," + RM692AX_B4.Text + "\r\n";
                    str = str + "R5," + RM692AX_R5.Text + "," + "G5," + RM692AX_G5.Text + "," + "B5," + RM692AX_B5.Text + "\r\n";
                    str = str + "R6," + RM692AX_R6.Text + "," + "G6," + RM692AX_G6.Text + "," + "B6," + RM692AX_B6.Text + "\r\n";
                    str = str + "R7," + RM692AX_R7.Text + "," + "G7," + RM692AX_G7.Text + "," + "B7," + RM692AX_B7.Text + "\r\n";
                    str = str + "R8," + RM692AX_R8.Text + "," + "G8," + RM692AX_G8.Text + "," + "B8," + RM692AX_B8.Text + "\r\n";
                    str = str + "R9," + RM692AX_R9.Text + "," + "G9," + RM692AX_G9.Text + "," + "B9," + RM692AX_B9.Text + "\r\n";
                    str = str + "R10," + RM692AX_R10.Text + "," + "G10," + RM692AX_G10.Text + "," + "B10," + RM692AX_B10.Text + "\r\n";
                    str = str + "R11," + RM692AX_R11.Text + "," + "G11," + RM692AX_G11.Text + "," + "B11," + RM692AX_B11.Text + "\r\n";
                    str = str + "R12," + RM692AX_R12.Text + "," + "G12," + RM692AX_G12.Text + "," + "B12," + RM692AX_B12.Text + "\r\n";
                    str = str + "R13," + RM692AX_R13.Text + "," + "G13," + RM692AX_G13.Text + "," + "B13," + RM692AX_B13.Text + "\r\n";
                    str = str + "R14," + RM692AX_R14.Text + "," + "G14," + RM692AX_G14.Text + "," + "B14," + RM692AX_B14.Text + "\r\n";
                    str = str + "R15," + RM692AX_R15.Text + "," + "G15," + RM692AX_G15.Text + "," + "B15," + RM692AX_B15.Text + "\r\n";
                    str = str + "R16," + RM692AX_R16.Text + "," + "G16," + RM692AX_G16.Text + "," + "B16," + RM692AX_B16.Text + "\r\n";
                    str = str + "R17," + RM692AX_R17.Text + "," + "G17," + RM692AX_G17.Text + "," + "B17," + RM692AX_B17.Text + "\r\n";
                    str = str + "R18," + RM692AX_R18.Text + "," + "G18," + RM692AX_G18.Text + "," + "B18," + RM692AX_B18.Text + "\r\n";
                    str = str + "R19," + RM692AX_R19.Text + "," + "G19," + RM692AX_G19.Text + "," + "B19," + RM692AX_B19.Text + "\r\n";
                    str = str + "R20," + RM692AX_R20.Text + "," + "G20," + RM692AX_G20.Text + "," + "B20," + RM692AX_B20.Text + "\r\n";
                    str = str + "R21," + RM692AX_R21.Text + "," + "G21," + RM692AX_G21.Text + "," + "B21," + RM692AX_B21.Text + "\r\n";
                    str = str + "R22," + RM692AX_R22.Text + "," + "G22," + RM692AX_G22.Text + "," + "B22," + RM692AX_B22.Text + "\r\n";
                    str = str + "R23," + RM692AX_R23.Text + "," + "G23," + RM692AX_G23.Text + "," + "B23," + RM692AX_B23.Text + "\r\n";
                    str = str + "R24," + RM692AX_R24.Text + "," + "G24," + RM692AX_G24.Text + "," + "B24," + RM692AX_B24.Text + "\r\n";
                    str = str + "R25," + RM692AX_R25.Text + "," + "G25," + RM692AX_G25.Text + "," + "B25," + RM692AX_B25.Text + "\r\n";
                    str = str + "R26," + RM692AX_R26.Text + "," + "G26," + RM692AX_G26.Text + "," + "B26," + RM692AX_B26.Text + "\r\n";
                    str = str + "R27," + RM692AX_R27.Text + "," + "G27," + RM692AX_G27.Text + "," + "B27," + RM692AX_B27.Text + "\r\n";
                    str = str + "R28," + RM692AX_R28.Text + "," + "G28," + RM692AX_G28.Text + "," + "B28," + RM692AX_B28.Text + "\r\n";
                    str = str + "R29," + RM692AX_R29.Text + "," + "G29," + RM692AX_G29.Text + "," + "B29," + RM692AX_B29.Text + "\r\n";
                    str = str + "R30," + RM692AX_R30.Text + "," + "G30," + RM692AX_G30.Text + "," + "B30," + RM692AX_B30.Text + "\r\n";
                    str = str + "R31," + RM692AX_R31.Text + "," + "G31," + RM692AX_G31.Text + "," + "B31," + RM692AX_B31.Text + "\r\n";
                    str = str + "R32," + RM692AX_R32.Text + "," + "G32," + RM692AX_G32.Text + "," + "B32," + RM692AX_B32.Text + "\r\n";

                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void RM692Ax_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                RM692AX_R0.Text = arrtmp[1].ToString(); RM692AX_G0.Text = arrtmp[3].ToString(); RM692AX_B0.Text = arrtmp[5].ToString();
                RM692AX_R1.Text = arrtmp[7].ToString(); RM692AX_G1.Text = arrtmp[9].ToString(); RM692AX_B1.Text = arrtmp[11].ToString();
                RM692AX_R2.Text = arrtmp[13].ToString(); RM692AX_G2.Text = arrtmp[15].ToString(); RM692AX_B2.Text = arrtmp[17].ToString();
                RM692AX_R3.Text = arrtmp[19].ToString(); RM692AX_G3.Text = arrtmp[21].ToString(); RM692AX_B3.Text = arrtmp[23].ToString();
                RM692AX_R4.Text = arrtmp[25].ToString(); RM692AX_G4.Text = arrtmp[27].ToString(); RM692AX_B4.Text = arrtmp[29].ToString();
                RM692AX_R5.Text = arrtmp[31].ToString(); RM692AX_G5.Text = arrtmp[33].ToString(); RM692AX_B5.Text = arrtmp[35].ToString();
                RM692AX_R6.Text = arrtmp[37].ToString(); RM692AX_G6.Text = arrtmp[39].ToString(); RM692AX_B6.Text = arrtmp[41].ToString();
                RM692AX_R7.Text = arrtmp[43].ToString(); RM692AX_G7.Text = arrtmp[45].ToString(); RM692AX_B7.Text = arrtmp[47].ToString();
                RM692AX_R8.Text = arrtmp[49].ToString(); RM692AX_G8.Text = arrtmp[51].ToString(); RM692AX_B8.Text = arrtmp[53].ToString();
                RM692AX_R9.Text = arrtmp[55].ToString(); RM692AX_G9.Text = arrtmp[57].ToString(); RM692AX_B9.Text = arrtmp[59].ToString();
                RM692AX_R10.Text = arrtmp[61].ToString(); RM692AX_G10.Text = arrtmp[63].ToString(); RM692AX_B10.Text = arrtmp[65].ToString();
                RM692AX_R11.Text = arrtmp[67].ToString(); RM692AX_G11.Text = arrtmp[69].ToString(); RM692AX_B11.Text = arrtmp[71].ToString();
                RM692AX_R12.Text = arrtmp[73].ToString(); RM692AX_G12.Text = arrtmp[75].ToString(); RM692AX_B12.Text = arrtmp[77].ToString();
                RM692AX_R13.Text = arrtmp[79].ToString(); RM692AX_G13.Text = arrtmp[81].ToString(); RM692AX_B13.Text = arrtmp[83].ToString();
                RM692AX_R14.Text = arrtmp[85].ToString(); RM692AX_G14.Text = arrtmp[87].ToString(); RM692AX_B14.Text = arrtmp[89].ToString();
                RM692AX_R15.Text = arrtmp[91].ToString(); RM692AX_G15.Text = arrtmp[93].ToString(); RM692AX_B15.Text = arrtmp[95].ToString();
                RM692AX_R16.Text = arrtmp[97].ToString(); RM692AX_G16.Text = arrtmp[99].ToString(); RM692AX_B16.Text = arrtmp[101].ToString();
                RM692AX_R17.Text = arrtmp[103].ToString(); RM692AX_G17.Text = arrtmp[105].ToString(); RM692AX_B17.Text = arrtmp[107].ToString();
                RM692AX_R18.Text = arrtmp[109].ToString(); RM692AX_G18.Text = arrtmp[111].ToString(); RM692AX_B18.Text = arrtmp[113].ToString();
                RM692AX_R19.Text = arrtmp[115].ToString(); RM692AX_G19.Text = arrtmp[117].ToString(); RM692AX_B19.Text = arrtmp[119].ToString();
                RM692AX_R20.Text = arrtmp[121].ToString(); RM692AX_G20.Text = arrtmp[123].ToString(); RM692AX_B20.Text = arrtmp[125].ToString();
                RM692AX_R21.Text = arrtmp[127].ToString(); RM692AX_G21.Text = arrtmp[129].ToString(); RM692AX_B21.Text = arrtmp[131].ToString();
                RM692AX_R22.Text = arrtmp[133].ToString(); RM692AX_G22.Text = arrtmp[135].ToString(); RM692AX_B22.Text = arrtmp[137].ToString();
                RM692AX_R23.Text = arrtmp[139].ToString(); RM692AX_G23.Text = arrtmp[141].ToString(); RM692AX_B23.Text = arrtmp[143].ToString();
                RM692AX_R24.Text = arrtmp[145].ToString(); RM692AX_G24.Text = arrtmp[147].ToString(); RM692AX_B24.Text = arrtmp[149].ToString();
                RM692AX_R25.Text = arrtmp[151].ToString(); RM692AX_G25.Text = arrtmp[153].ToString(); RM692AX_B25.Text = arrtmp[155].ToString();
                RM692AX_R26.Text = arrtmp[157].ToString(); RM692AX_G26.Text = arrtmp[159].ToString(); RM692AX_B26.Text = arrtmp[161].ToString();
                RM692AX_R27.Text = arrtmp[163].ToString(); RM692AX_G27.Text = arrtmp[165].ToString(); RM692AX_B27.Text = arrtmp[167].ToString();
                RM692AX_R28.Text = arrtmp[169].ToString(); RM692AX_G28.Text = arrtmp[171].ToString(); RM692AX_B28.Text = arrtmp[173].ToString();
                RM692AX_R29.Text = arrtmp[175].ToString(); RM692AX_G29.Text = arrtmp[177].ToString(); RM692AX_B29.Text = arrtmp[179].ToString();
                RM692AX_R30.Text = arrtmp[181].ToString(); RM692AX_G30.Text = arrtmp[183].ToString(); RM692AX_B30.Text = arrtmp[185].ToString();
                RM692AX_R31.Text = arrtmp[187].ToString(); RM692AX_G31.Text = arrtmp[189].ToString(); RM692AX_B31.Text = arrtmp[191].ToString();
                RM692AX_R32.Text = arrtmp[193].ToString(); RM692AX_G32.Text = arrtmp[195].ToString(); RM692AX_B32.Text = arrtmp[197].ToString();
            }
        }
        private void RM692AX_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[40];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 40;

            Send_buf[0] = 0x3E;        //RM692AX
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM692Ax_RegR":
                    {
                        string strLine = Convert.ToString(RM692AX_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                        int DataNum = Convert.ToByte(tb_RM692Ax_return_num.Text);
                        Setting.TempRecord.cmdLen = DataNum + 1 + 5;
                        Send_buf = new byte[DataNum + 1 + 5];

                        Send_buf[0] = 0x3E;
                        Send_buf[1] = 0x02; //read function
                        Send_buf[2] = Convert.ToByte(DataNum + 1);//length
                        Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register
                    }
                    break;
                case "RM692Ax_RegW":
                    {
                        string strLine = Convert.ToString(RM692AX_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组

                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x3E; //
                        Send_buf[1] = 0x01; //寄存器写
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                    }
                    break;
                case "btn_RM692Ax_gamma_point_select":
                    {
                        Send_buf[0] = 0x3E;
                        Send_buf[1] = 0x06; //重置绑点位置
                        Send_buf[2] = 0x23; //33+2cmd = 35;
                        /*
                        Send_buf[3] = Convert.ToByte(RM692Ax_Ponit_Num.Value); //绑点个数
                        Send_buf[4] = Convert.ToByte(RM692AX_0.Value);
                        Send_buf[5] = Convert.ToByte(RM692AX_1.Value);
                        Send_buf[6] = Convert.ToByte(RM692AX_2.Value);
                        Send_buf[7] = Convert.ToByte(RM692AX_3.Value);
                        Send_buf[8] = Convert.ToByte(RM692AX_4.Value);
                        Send_buf[9] = Convert.ToByte(RM692AX_5.Value);
                        Send_buf[10] = Convert.ToByte(RM692AX_6.Value);
                        Send_buf[11] = Convert.ToByte(RM692AX_7.Value);
                        Send_buf[12] = Convert.ToByte(RM692AX_8.Value);
                        Send_buf[13] = Convert.ToByte(RM692AX_9.Value);
                        Send_buf[14] = Convert.ToByte(RM692AX_10.Value);
                        Send_buf[15] = Convert.ToByte(RM692AX_11.Value);
                        Send_buf[16] = Convert.ToByte(RM692AX_12.Value);
                        Send_buf[17] = Convert.ToByte(RM692AX_13.Value);
                        Send_buf[18] = Convert.ToByte(RM692AX_14.Value);
                        Send_buf[19] = Convert.ToByte(RM692AX_15.Value);
                        Send_buf[20] = Convert.ToByte(RM692AX_16.Value);
                        Send_buf[21] = Convert.ToByte(RM692AX_17.Value);
                        Send_buf[22] = Convert.ToByte(RM692AX_18.Value);
                        Send_buf[23] = Convert.ToByte(RM692AX_19.Value);
                        Send_buf[24] = Convert.ToByte(RM692AX_20.Value);
                        Send_buf[25] = Convert.ToByte(RM692AX_21.Value);
                        Send_buf[26] = Convert.ToByte(RM692AX_22.Value);
                        Send_buf[27] = Convert.ToByte(RM692AX_23.Value);
                        Send_buf[28] = Convert.ToByte(RM692AX_24.Value);
                        Send_buf[29] = Convert.ToByte(RM692AX_25.Value);
                        Send_buf[30] = Convert.ToByte(RM692AX_26.Value);
                        Send_buf[31] = Convert.ToByte(RM692AX_27.Value);
                        Send_buf[32] = Convert.ToByte(RM692AX_28.Value);
                        Send_buf[33] = Convert.ToByte(RM692AX_29.Value);
                        Send_buf[34] = Convert.ToByte(RM692AX_30.Value);
                        Send_buf[35] = Convert.ToByte(RM692AX_31.Value);
                        Send_buf[36] = Convert.ToByte(RM692AX_32.Value);
                        */
                    }
                    break;
                case "RM692Ax_Reg_OTP":       //          B
                    {
                        Setting.TempRecord.cmdLen = 8;
                        Send_buf = new byte[8];
                        Send_buf[0] = 0x3E;
                        Send_buf[1] = 0x07; //OTP function
                        Send_buf[2] = 3;//length
                        Send_buf[3] = Convert.ToByte(RM692AX_Time.Text);
                        if (RM692Ax_Page50_rbt.Checked == true)
                        {
                            Send_buf[4] = 1;
                            if (cbx_RM692Ax_Gamma1.Checked == true)
                            {
                                Send_buf[5] = 1;
                            }
                            else if (cbx_RM692Ax_Gamma5.Checked == true)
                            {
                                Send_buf[5] = 5;
                            }
                            else if (cbx_RM692Ax_Gamma9.Checked == true)
                            {
                                Send_buf[5] = 9;
                            }
                            else if (cbx_RM692Ax_Gamma13.Checked == true)
                            {
                                Send_buf[5] = 13;
                            }
                        }
                        else if (RM692Ax_Page52_rbt.Checked == true)
                        {
                            Send_buf[4] = 2;
                            if (cbx_RM692Ax_Gamma2.Checked == true)
                            {
                                Send_buf[5] = 2;
                            }
                            else if (cbx_RM692Ax_Gamma6.Checked == true)
                            {
                                Send_buf[5] = 6;
                            }
                            else if (cbx_RM692Ax_Gamma10.Checked == true)
                            {
                                Send_buf[5] = 10;
                            }
                        }
                        else if (RM692Ax_Page54_rbt.Checked == true)
                        {
                            Send_buf[4] = 3;
                            if (cbx_RM692Ax_Gamma3.Checked == true)
                            {
                                Send_buf[5] = 3;
                            }
                            else if (cbx_RM692Ax_Gamma7.Checked == true)
                            {
                                Send_buf[5] = 7;
                            }
                        }
                        else if (RM692Ax_Page56_rbt.Checked == true)
                        {
                            Send_buf[4] = 4;
                            if (cbx_RM692Ax_Gamma4.Checked == true)
                            {
                                Send_buf[5] = 4;
                            }
                            else if (cbx_RM692Ax_Gamma8.Checked == true)
                            {
                                Send_buf[5] = 8;
                            }
                        }
                        else if (RM692Ax_PageN_rbt.Checked == true)
                        {
                            Send_buf[4] = 5;
                        }
                        else if (RM692Ax_PageD_rbt.Checked == true)
                        {
                            Send_buf[4] = 6;
                        }
                        else if (RM692Ax_PageRCN_rbt.Checked == true)
                        {
                            Send_buf[4] = 7;
                        }
                    }
                    break;
                case "OTP_Time_W":         //w写寄存器   C
                    {
                        if (Convert.ToDecimal(RM692AX_Time.Text) > 4)
                        {
                            MessageBox.Show("OTP次数不能大于4次", "警告");
                            return;
                        }
                        else
                        {
                            Send_buf[1] = 0x0C;
                            Send_buf[4] = byte.Parse((Convert.ToInt32(RM692AX_Time.Text.Trim(), 16)).ToString());
                        }
                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 3000;
            frmHelp.T1.Enabled = true;
        }
        private void RM692AX_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[74];
            Setting.TempRecord.cmdLen = 74;
            Send_buf[2] = 69;
            Send_buf[3] = 0;
            Send_buf[0] = 0x3E; //RM692AX

            switch (btnTmp.Name)
            {
                case "RM692Ax_GammaR_R":
                    {
                        Setting.TempRecord.RM692AXCmdType = 0x03;       //      3
                        Send_buf[1] = 0x04;                             //读 Red Gamma
                        Send_buf[3] = 0x00;
                        if (RM692Ax_Page50_rbt.Checked == true) { Send_buf[4] = 0x01; }
                        else if (RM692Ax_Page52_rbt.Checked == true) { Send_buf[4] = 0x02; }
                        else if (RM692Ax_Page54_rbt.Checked == true) { Send_buf[4] = 0x03; }
                        else if (RM692Ax_Page56_rbt.Checked == true) { Send_buf[4] = 0x04; }
                    }
                    break;
                case "RM692Ax_GammaR_G":
                    {
                        Setting.TempRecord.RM692AXCmdType = 0x04;   //      4
                        Send_buf[1] = 0x04;                         //  读Gamma
                        Send_buf[3] = 0x01;                        //绿色 green
                        if (RM692Ax_Page50_rbt.Checked == true) { Send_buf[4] = 0x01; }
                        else if (RM692Ax_Page52_rbt.Checked == true) { Send_buf[4] = 0x02; }
                        else if (RM692Ax_Page54_rbt.Checked == true) { Send_buf[4] = 0x03; }
                        else if (RM692Ax_Page56_rbt.Checked == true) { Send_buf[4] = 0x04; }
                    }
                    break;
                case "RM692Ax_GammaR_B": //              9
                    {
                        Setting.TempRecord.RM692AXCmdType = 0x05;   //      5
                        Send_buf[1] = 0x04;                         //   读Gamma
                        Send_buf[3] = 0x02;                        //   blue
                        if (RM692Ax_Page50_rbt.Checked == true) { Send_buf[4] = 0x01; }
                        else if (RM692Ax_Page52_rbt.Checked == true) { Send_buf[4] = 0x02; }
                        else if (RM692Ax_Page54_rbt.Checked == true) { Send_buf[4] = 0x03; }
                        else if (RM692Ax_Page56_rbt.Checked == true) { Send_buf[4] = 0x04; }
                    }
                    break;
                //--------------------------------------
                case "RM692Ax_GammaW_R":
                    {
                        Send_buf[1] = 0x03;
                        if (RM692Ax_Page50_rbt.Checked == true) { Send_buf[3] = 0x00; }
                        else if (RM692Ax_Page52_rbt.Checked == true) { Send_buf[3] = 0x01; }
                        else if (RM692Ax_Page54_rbt.Checked == true) { Send_buf[3] = 0x02; }
                        else if (RM692Ax_Page56_rbt.Checked == true) { Send_buf[3] = 0x03; }
                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM692AX_R0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM692AX_R0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692AX_R1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692AX_R1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692AX_R2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692AX_R2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692AX_R3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692AX_R3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692AX_R4.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692AX_R4.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692AX_R5.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692AX_R5.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692AX_R6.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692AX_R6.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692AX_R7.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692AX_R7.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692AX_R8.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692AX_R8.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692AX_R9.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692AX_R9.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692AX_R10.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692AX_R10.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692AX_R11.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692AX_R11.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692AX_R12.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692AX_R12.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692AX_R13.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692AX_R13.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692AX_R14.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692AX_R14.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692AX_R15.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692AX_R15.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692AX_R16.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692AX_R16.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692AX_R17.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692AX_R17.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692AX_R18.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692AX_R18.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692AX_R19.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692AX_R19.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692AX_R20.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692AX_R20.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692AX_R21.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692AX_R21.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692AX_R22.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692AX_R22.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692AX_R23.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692AX_R23.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692AX_R24.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692AX_R24.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692AX_R25.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692AX_R25.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692AX_R26.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692AX_R26.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692AX_R27.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692AX_R27.Value.ToString())) & 0xff);

                        Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692AX_R28.Value.ToString()) >> 8);
                        Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692AX_R28.Value.ToString())) & 0xff);
                        Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692AX_R29.Value.ToString()) >> 8);
                        Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692AX_R29.Value.ToString())) & 0xff);
                        Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692AX_R30.Value.ToString()) >> 8);
                        Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692AX_R30.Value.ToString())) & 0xff);
                        Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692AX_R31.Value.ToString()) >> 8);
                        Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692AX_R31.Value.ToString())) & 0xff);
                        Send_buf[68] = Convert.ToByte(Convert.ToUInt16(RM692AX_R32.Value.ToString()) >> 8);
                        Send_buf[69] = Convert.ToByte((Convert.ToUInt16(RM692AX_R32.Value.ToString())) & 0xff);
                        Send_buf[70] = Convert.ToByte(Convert.ToUInt16(RM692AX_RFPS.Value.ToString()) >> 8);
                        Send_buf[71] = Convert.ToByte((Convert.ToUInt16(RM692AX_RFPS.Value.ToString())) & 0xff);
                    }
                    break;
                case "RM692Ax_GammaW_G":
                    {
                        Send_buf[1] = 0x03;
                        if (RM692Ax_Page50_rbt.Checked == true) { Send_buf[3] = 0x40; }
                        else if (RM692Ax_Page52_rbt.Checked == true) { Send_buf[3] = 0x41; }
                        else if (RM692Ax_Page54_rbt.Checked == true) { Send_buf[3] = 0x42; }
                        else if (RM692Ax_Page56_rbt.Checked == true) { Send_buf[3] = 0x43; }
                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM692AX_G0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM692AX_G0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692AX_G1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692AX_G1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692AX_G2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692AX_G2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692AX_G3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692AX_G3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692AX_G4.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692AX_G4.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692AX_G5.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692AX_G5.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692AX_G6.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692AX_G6.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692AX_G7.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692AX_G7.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692AX_G8.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692AX_G8.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692AX_G9.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692AX_G9.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692AX_G10.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692AX_G10.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692AX_G11.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692AX_G11.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692AX_G12.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692AX_G12.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692AX_G13.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692AX_G13.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692AX_G14.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692AX_G14.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692AX_G15.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692AX_G15.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692AX_G16.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692AX_G16.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692AX_G17.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692AX_G17.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692AX_G18.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692AX_G18.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692AX_G19.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692AX_G19.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692AX_G20.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692AX_G20.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692AX_G21.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692AX_G21.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692AX_G22.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692AX_G22.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692AX_G23.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692AX_G23.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692AX_G24.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692AX_G24.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692AX_G25.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692AX_G25.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692AX_G26.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692AX_G26.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692AX_G27.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692AX_G27.Value.ToString())) & 0xff);
                        Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692AX_G28.Value.ToString()) >> 8);
                        Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692AX_G28.Value.ToString())) & 0xff);
                        Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692AX_G29.Value.ToString()) >> 8);
                        Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692AX_G29.Value.ToString())) & 0xff);
                        Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692AX_G30.Value.ToString()) >> 8);
                        Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692AX_G30.Value.ToString())) & 0xff);
                        Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692AX_G31.Value.ToString()) >> 8);
                        Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692AX_G31.Value.ToString())) & 0xff);
                        Send_buf[68] = Convert.ToByte(Convert.ToUInt16(RM692AX_G32.Value.ToString()) >> 8);
                        Send_buf[69] = Convert.ToByte((Convert.ToUInt16(RM692AX_G32.Value.ToString())) & 0xff);
                        Send_buf[70] = Convert.ToByte(Convert.ToUInt16(RM692AX_GFPS.Value.ToString()) >> 8);
                        Send_buf[71] = Convert.ToByte((Convert.ToUInt16(RM692AX_GFPS.Value.ToString())) & 0xff);
                    }
                    break;
                case "RM692Ax_GammaW_B":     //      8
                    {
                        Send_buf[1] = 0x03;
                        if (RM692Ax_Page50_rbt.Checked == true) { Send_buf[3] = 0x80; }
                        else if (RM692Ax_Page52_rbt.Checked == true) { Send_buf[3] = 0x81; }
                        else if (RM692Ax_Page54_rbt.Checked == true) { Send_buf[3] = 0x82; }
                        else if (RM692Ax_Page56_rbt.Checked == true) { Send_buf[3] = 0x83; }
                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM692AX_B0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM692AX_B0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692AX_B1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692AX_B1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692AX_B2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692AX_B2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692AX_B3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692AX_B3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692AX_B4.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692AX_B4.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692AX_B5.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692AX_B5.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692AX_B6.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692AX_B6.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692AX_B7.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692AX_B7.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692AX_B8.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692AX_B8.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692AX_B9.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692AX_B9.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692AX_B10.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692AX_B10.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692AX_B11.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692AX_B11.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692AX_B12.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692AX_B12.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692AX_B13.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692AX_B13.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692AX_B14.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692AX_B14.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692AX_B15.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692AX_B15.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692AX_B16.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692AX_B16.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692AX_B17.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692AX_B17.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692AX_B18.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692AX_B18.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692AX_B19.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692AX_B19.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692AX_B20.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692AX_B20.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692AX_B21.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692AX_B21.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692AX_B22.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692AX_B22.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692AX_B23.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692AX_B23.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692AX_B24.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692AX_B24.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692AX_B25.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692AX_B25.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692AX_B26.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692AX_B26.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692AX_B27.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692AX_B27.Value.ToString())) & 0xff);
                        Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692AX_B28.Value.ToString()) >> 8);
                        Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692AX_B28.Value.ToString())) & 0xff);
                        Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692AX_B29.Value.ToString()) >> 8);
                        Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692AX_B29.Value.ToString())) & 0xff);
                        Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692AX_B30.Value.ToString()) >> 8);
                        Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692AX_B30.Value.ToString())) & 0xff);
                        Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692AX_B31.Value.ToString()) >> 8);
                        Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692AX_B31.Value.ToString())) & 0xff);
                        Send_buf[68] = Convert.ToByte(Convert.ToUInt16(RM692AX_B32.Value.ToString()) >> 8);
                        Send_buf[69] = Convert.ToByte((Convert.ToUInt16(RM692AX_B32.Value.ToString())) & 0xff);
                        Send_buf[70] = Convert.ToByte(Convert.ToUInt16(RM692AX_BFPS.Value.ToString()) >> 8);
                        Send_buf[71] = Convert.ToByte((Convert.ToUInt16(RM692AX_BFPS.Value.ToString())) & 0xff);
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[72], out Send_buf[73]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 74);   //RM69350
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void btnCal_RM692Ax_Position_Click(object sender, EventArgs e)
        {
            double Lr_d, Lg_d, Lb_d;

            string[] arr = RM692Ax_Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            //----------------------------------------------------------------------------
            try
            {
                txt2AX_lumoff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txt2AX_Xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txt2AX_Yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                int Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];
                txt2AX_Gamma.Text = Diven_Gamma[0].Curve.ToString();
                txt2AX_lum.Text = Diven_Gamma[0].Lum.ToString();
                txt2AX_X.Text = Diven_Gamma[0].X.ToString();
                txt2AX_Y.Text = Diven_Gamma[0].Y.ToString();

                for (int p = 0; p < Total_points; p++)
                {
                    D_lum[p] = (Math.Pow((Diven_Offset[p].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                }

                CaSDKObj.YUV_to_RGB(Diven_Gamma[0].Y, Diven_Gamma[0].X, out Lr_d, out Lb_d, out Lg_d);//R/B/G分亮度转换

                for (int i = 0; i < Total_points; i++)
                {
                    D_lums[i, 0] = D_lum[i] * Lr_d; //R/B/G分亮度计算
                    D_lums[i, 1] = D_lum[i] * Lg_d;
                    D_lums[i, 2] = D_lum[i] * Lb_d;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }
        //自动调节启动开关
        private static byte Gamma_Auto_flag_dll = 0;
        private void btnAutoGamma_RM692Ax_Click(object sender, EventArgs e)//255阶为例
        {
            Button btnTmp = (Button)sender;
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            Gamma_Auto_flag_dll = 1;

            btnCal_RM692Ax_Position_Click(null, null);

            gamma_r = GAMMAs.RM692AX_Gamma_r;
            gamma_g = GAMMAs.RM692AX_Gamma_g;
            gamma_b = GAMMAs.RM692AX_Gamma_b;

            if (cbx_FODGamma.Checked == true)
            {
                RM692AX_R32.Value = 0; RM692AX_B32.Value = 0;
                RM692AX_R31.Value = 0; RM692AX_B31.Value = 0;
                RM692AX_R30.Value = 0; RM692AX_B30.Value = 0;
                RM692AX_R29.Value = 0; RM692AX_B29.Value = 0;
                RM692AX_R28.Value = 0; RM692AX_B28.Value = 0;
                RM692AX_R27.Value = 0; RM692AX_B27.Value = 0;
                RM692AX_R26.Value = 0; RM692AX_B26.Value = 0;
                RM692AX_R25.Value = 0; RM692AX_B25.Value = 0;
                RM692AX_R24.Value = 0; RM692AX_B24.Value = 0;
                RM692AX_R23.Value = 0; RM692AX_B23.Value = 0;
                RM692AX_R22.Value = 0; RM692AX_B22.Value = 0;
                RM692AX_R21.Value = 0; RM692AX_B21.Value = 0;
                RM692AX_R20.Value = 0; RM692AX_B20.Value = 0;
                RM692AX_R19.Value = 0; RM692AX_B19.Value = 0;
                RM692AX_R18.Value = 0; RM692AX_B18.Value = 0;
                RM692AX_R17.Value = 0; RM692AX_B17.Value = 0;
                RM692AX_R16.Value = 0; RM692AX_B16.Value = 0;
                RM692AX_R15.Value = 0; RM692AX_B15.Value = 0;
                RM692AX_R14.Value = 0; RM692AX_B14.Value = 0;
                RM692AX_R13.Value = 0; RM692AX_B13.Value = 0;
                RM692AX_R12.Value = 0; RM692AX_B12.Value = 0;
                RM692AX_R11.Value = 0; RM692AX_B11.Value = 0;
                RM692AX_R10.Value = 0; RM692AX_B10.Value = 0;
                RM692AX_R9.Value = 0; RM692AX_B9.Value = 0;
                RM692AX_R8.Value = 0; RM692AX_B8.Value = 0;
                RM692AX_R7.Value = 0; RM692AX_B7.Value = 0;
                RM692AX_R6.Value = 0; RM692AX_B6.Value = 0;
                RM692AX_R5.Value = 0; RM692AX_B5.Value = 0;
                RM692AX_R4.Value = 0; RM692AX_B4.Value = 0;
                RM692AX_R3.Value = 0; RM692AX_B3.Value = 0;
                RM692AX_R2.Value = 0; RM692AX_B2.Value = 0;
                RM692AX_R1.Value = 0; RM692AX_B1.Value = 0;
                RM692AX_R0.Value = 0; RM692AX_B0.Value = 0;
                frmHelp.Red_Ratio = 0x0;
                frmHelp.Green_Ratio = 0xf;
                frmHelp.Blue_Ratio = 0x0;
            }
            else
            {
                frmHelp.Red_Ratio = 0xf;
                frmHelp.Green_Ratio = 0xf;
                frmHelp.Blue_Ratio = 0xf;
            }
            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txt2AX_lumoff.Text);
                        x_off = Convert.ToDouble(txt2AX_Xoff.Text);
                        y_off = Convert.ToDouble(txt2AX_Yoff.Text);
                        Application.DoEvents();

                        if (cbx_FODGamma.Checked == true)
                        {
                            frmHelp.checkBox1.Checked = false;
                            frmHelp.numPatternR.Value = 0;
                            frmHelp.numPatternG.Value = Diven_Offset[0].NO;
                            frmHelp.numPatternB.Value = 0;
                        }
                        else
                        {
                            frmHelp.checkBox1.Checked = true;
                            frmHelp.numPatternR.Value = Diven_Offset[0].NO;//固定255灰阶
                            frmHelp.numPatternG.Value = Diven_Offset[0].NO;
                            frmHelp.numPatternB.Value = Diven_Offset[0].NO;
                        }

                        Application.DoEvents();
                        Thread.Sleep(10);
                        if (cBx_UDC_Auto_Gamma.Checked == true)
                        {
                            Load_GammmPic(Diven_Offset[0].NO.ToString()); //Load pic
                        }
                        else
                        {
                            frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                        }
                        Thread.Sleep(10); //1秒=1000毫秒，

                        T20.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        private void Load_GammmPic(string filenames)      //双击 载入 BMP图片
        {
            Int32 HACT_True = 0;
            Int32 HACT_Skip = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            FileStream aFile;

            //byte[] TemFileContent = new byte[1024 * 100];
            byte[] TemFileContent = new byte[512 * 4000 + 100];
            System.Int32 ByteReaded = 0x00;

            string FileName = Gamma_loading_path.Text + '\\' + filenames + ".bmp";
            aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);

            HACT_Skip = HACT_True;
            try
            { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                frmHelp.comPort.Write(TemFileContent, 0, 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            //-----------------------------------
            try
            {
                j = 1;
                aFile.Seek(54, SeekOrigin.Begin);
                while (j <= VACT_True)
                {
                    if (HACT_True % 4 != 0)
                        aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                    else
                        aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);

                    ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                    try
                    {
                        frmHelp.comPort.Write(TemFileContent, 0, HACT_Skip * 3);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        break;
                    }
                    m_txlen += ByteReaded;
                    //     frmHelp.progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                    //     frmHelp.label14.Text = frmHelp.progressBar1.Value.ToString() + "%";
                    //    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                    j++;
                }

                TemFileContent[0] = 0x5A;
                try
                {
                    frmHelp.comPort.Write(TemFileContent, 0, 1);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                aFile.Close();
            }
            catch
            {
                frmHelp.textBox1.Text += "啥图片都没显示";
                frmHelp.textBox1.Select(frmHelp.textBox1.Text.Length, 0);
                frmHelp.textBox1.ScrollToCaret();
            }
        }

        private void RM692Ax_AUTO_gamma(Object myObject, EventArgs myEventArgs)
        {
            int total_points = Diven_Gamma[0].Points_num;
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if (cbx_FODGamma.Checked == true)
                {
                    if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                    { flag1 = 0; }
                    else
                    { flag1 = 1; }
                    flag2 = 0;
                    flag3 = 0;
                }
                else
                {
                    if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                    { flag1 = 0; }
                    else
                    { flag1 = 1; }
                    if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                    { flag3 = 0; }
                    else
                    { flag3 = 1; }
                    if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                    { flag2 = 0; }
                    else
                    { flag2 = 1; }
                }
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;
                if (index == total_points)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Ax gamma调试完毕!\r\n";
                }
                if (index < total_points)
                {
                    if (cbx_FODGamma.Checked == true)
                    {
                        frmHelp.numPatternR.Value = 0;
                        frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                        frmHelp.numPatternB.Value = 0;
                    }
                    else
                    {
                        frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                        frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                        frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                    }

                    Application.DoEvents();
                    if (cBx_UDC_Auto_Gamma.Checked == true)
                    {
                        Load_GammmPic(Diven_Offset[index].NO.ToString()); //Load pic
                    }
                    else
                    {
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    }

                    Thread.Sleep(500); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else  //调整部分
            {
                decimal r = 0, g = 0, b = 0;
                int flexible_gamma_count = 120;
                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);
                if (cbx_FODGamma.Checked == true)
                {
                    D_lums[index, 0] = D_lum[index]; //R/B/G分亮度计算
                    D_lums[index, 1] = D_lum[index];
                    D_lums[index, 2] = D_lum[index];
                    lumr = frmHelp.CASDK_LUM;
                    lumg = frmHelp.CASDK_LUM;
                    lumb = frmHelp.CASDK_LUM;

                }
                else
                {
                    lumr = frmHelp.CASDK_LUM * Lr_t;
                    lumg = frmHelp.CASDK_LUM * Lg_t;
                    lumb = frmHelp.CASDK_LUM * Lb_t;
                }

                try
                {
                    switch (index)
                    {
                        case 31: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R32.Value, RM692AX_G32.Value, RM692AX_B32.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R32.Value = r;
                                RM692AX_G32.Value = g;
                                RM692AX_B32.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R32.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G32.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B32.Value));
                            }
                            break;
                        case 32: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R31.Value, RM692AX_G31.Value, RM692AX_B31.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R31.Value = r;
                                RM692AX_G31.Value = g;
                                RM692AX_B31.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R31.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G31.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B31.Value));
                            }
                            break;
                        case 0: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R30.Value, RM692AX_G30.Value, RM692AX_B30.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R30.Value = r;
                                RM692AX_G30.Value = g;
                                RM692AX_B30.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R30.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G30.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B30.Value));
                            }
                            break;
                        case 1: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R29.Value, RM692AX_G29.Value, RM692AX_B29.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R29.Value = r;
                                RM692AX_G29.Value = g;
                                RM692AX_B29.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R29.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G29.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B29.Value));
                            }
                            break;
                        case 2: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R28.Value, RM692AX_G28.Value, RM692AX_B28.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R28.Value = r;
                                RM692AX_G28.Value = g;
                                RM692AX_B28.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R28.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G28.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B28.Value));
                            }
                            break;
                        case 3: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R27.Value, RM692AX_G27.Value, RM692AX_B27.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R27.Value = r;
                                RM692AX_G27.Value = g;
                                RM692AX_B27.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R27.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G27.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B27.Value));
                            }
                            break;
                        case 4: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R26.Value, RM692AX_G26.Value, RM692AX_B26.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R26.Value = r;
                                RM692AX_G26.Value = g;
                                RM692AX_B26.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R26.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G26.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B26.Value));
                            }
                            break;
                        case 5: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R25.Value, RM692AX_G25.Value, RM692AX_B25.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R25.Value = r;
                                RM692AX_G25.Value = g;
                                RM692AX_B25.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R25.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G25.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B25.Value));
                            }
                            break;
                        case 6: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R24.Value, RM692AX_G24.Value, RM692AX_B24.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R24.Value = r;
                                RM692AX_G24.Value = g;
                                RM692AX_B24.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R24.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G24.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B24.Value));
                            }
                            break;
                        case 7: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R23.Value, RM692AX_G23.Value, RM692AX_B23.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R23.Value = r;
                                RM692AX_G23.Value = g;
                                RM692AX_B23.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R23.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G23.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B23.Value));
                            }
                            break;
                        case 8: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R22.Value, RM692AX_G22.Value, RM692AX_B22.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R22.Value = r;
                                RM692AX_G22.Value = g;
                                RM692AX_B22.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R22.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G22.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B22.Value));
                            }
                            break;
                        case 9: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R21.Value, RM692AX_G21.Value, RM692AX_B21.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R21.Value = r;
                                RM692AX_G21.Value = g;
                                RM692AX_B21.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R21.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G21.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B21.Value));
                            }
                            break;
                        case 10: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R20.Value, RM692AX_G20.Value, RM692AX_B20.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R20.Value = r;
                                RM692AX_G20.Value = g;
                                RM692AX_B20.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R20.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G20.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B20.Value));
                            }
                            break;
                        case 11: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R19.Value, RM692AX_G19.Value, RM692AX_B19.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R19.Value = r;
                                RM692AX_G19.Value = g;
                                RM692AX_B19.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R19.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G19.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B19.Value));
                            }
                            break;
                        case 12: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R18.Value, RM692AX_G18.Value, RM692AX_B18.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R18.Value = r;
                                RM692AX_G18.Value = g;
                                RM692AX_B18.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R18.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G18.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B18.Value));
                            }
                            break;
                        case 13: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R17.Value, RM692AX_G17.Value, RM692AX_B17.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R17.Value = r;
                                RM692AX_G17.Value = g;
                                RM692AX_B17.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R17.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G17.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B17.Value));
                            }
                            break;
                        case 14: //
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R16.Value, RM692AX_G16.Value, RM692AX_B16.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R16.Value = r;
                                RM692AX_G16.Value = g;
                                RM692AX_B16.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R16.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G16.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B16.Value));
                            }
                            break;
                        case 15: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R15.Value, RM692AX_G15.Value, RM692AX_B15.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R15.Value = r;
                                RM692AX_G15.Value = g;
                                RM692AX_B15.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R15.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G15.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B15.Value));
                            }
                            break;
                        case 16: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R14.Value, RM692AX_G14.Value, RM692AX_B14.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R14.Value = r;
                                RM692AX_G14.Value = g;
                                RM692AX_B14.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R14.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G14.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B14.Value));
                            }
                            break;
                        case 17: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R13.Value, RM692AX_G13.Value, RM692AX_B13.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R13.Value = r;
                                RM692AX_G13.Value = g;
                                RM692AX_B13.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R13.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G13.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B13.Value));
                            }
                            break;
                        case 18: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R12.Value, RM692AX_G12.Value, RM692AX_B12.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R12.Value = r;
                                RM692AX_G12.Value = g;
                                RM692AX_B12.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R12.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G12.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B12.Value));
                            }
                            break;
                        case 19: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R11.Value, RM692AX_G11.Value, RM692AX_B11.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R11.Value = r;
                                RM692AX_G11.Value = g;
                                RM692AX_B11.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R11.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G11.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B11.Value));
                            }
                            break;
                        case 20: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R10.Value, RM692AX_G10.Value, RM692AX_B10.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R10.Value = r;
                                RM692AX_G10.Value = g;
                                RM692AX_B10.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R10.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G10.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B10.Value));
                            }
                            break;
                        case 21: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R9.Value, RM692AX_G9.Value, RM692AX_B9.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R9.Value = r;
                                RM692AX_G9.Value = g;
                                RM692AX_B9.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R9.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G9.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B9.Value));
                            }
                            break;
                        case 22: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R8.Value, RM692AX_G8.Value, RM692AX_B8.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R8.Value = r;
                                RM692AX_G8.Value = g;
                                RM692AX_B8.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R8.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G8.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B8.Value));
                            }
                            break;
                        case 23: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R7.Value, RM692AX_G7.Value, RM692AX_B7.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R7.Value = r;
                                RM692AX_G7.Value = g;
                                RM692AX_B7.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R7.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G7.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B7.Value));
                            }
                            break;
                        case 24: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R6.Value, RM692AX_G6.Value, RM692AX_B6.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R6.Value = r;
                                RM692AX_G6.Value = g;
                                RM692AX_B6.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R6.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G6.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B6.Value));
                            }
                            break;
                        case 25: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R5.Value, RM692AX_G5.Value, RM692AX_B5.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R5.Value = r;
                                RM692AX_G5.Value = g;
                                RM692AX_B5.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R5.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G5.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B5.Value));
                            }
                            break;
                        case 26: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R4.Value, RM692AX_G4.Value, RM692AX_B4.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R4.Value = r;
                                RM692AX_G4.Value = g;
                                RM692AX_B4.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R4.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G4.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B4.Value));
                            }
                            break;
                        case 27: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R3.Value, RM692AX_G3.Value, RM692AX_B3.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R3.Value = r;
                                RM692AX_G3.Value = g;
                                RM692AX_B3.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R3.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G3.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B3.Value));
                            }
                            break;
                        case 28: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R2.Value, RM692AX_G2.Value, RM692AX_B2.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R2.Value = r;
                                RM692AX_G2.Value = g;
                                RM692AX_B2.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R2.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G2.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B2.Value));
                            }
                            break;
                        case 29: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692AX_R1.Value, RM692AX_G1.Value, RM692AX_B1.Value, Diven_Offset[index].NO, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692AX_R1.Value = r;
                                RM692AX_G1.Value = g;
                                RM692AX_B1.Value = b;
                                frmHelp.auto_gamma_write(gamma_r[30 - index], Convert.ToUInt16(RM692AX_R1.Value), gamma_g[30 - index], Convert.ToUInt16(RM692AX_G1.Value), gamma_b[30 - index], Convert.ToUInt16(RM692AX_B1.Value));
                            }
                            break;
                        case 30: //
                            {
                                Gamma_adjust_timeout_flag = true;
                                RM692AX_R0.Value = 0;
                                RM692AX_G0.Value = 0;
                                RM692AX_B0.Value = 0;
                                Application.DoEvents();
                            }
                            break;
                        default: break;
                    }
                    /*
                        RM692AX_Gamma_Click(RM692Ax_GammaW_R, null);
                        Thread.Sleep(10);
                        RM692AX_Gamma_Click(RM692Ax_GammaW_G, null);
                        Thread.Sleep(10);
                        RM692AX_Gamma_Click(RM692Ax_GammaW_B, null);
                        Thread.Sleep(10);
                    */
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("数值超限！");
                }
            }
        }
        private void RM692Ax_trackBar_Scroll(object sender, EventArgs e)
        {
            int bits_51h = Convert.ToInt16(Math.Pow(2, Convert.ToByte(RM692AX_51h_Bits.Text))) - 1;
            label_RM692Ax_trackBar.Text = bits_51h + "阶";
            RM692Ax_trackBar.Maximum = bits_51h;

            RM692AX_Reg_Data.Text = "fe,00";
            RM692AX_Others_Click(RM692Ax_RegW, e);
            if (bits_51h < 0x100)
            {
                RM692AX_Reg_Data.Text = "51," + RM692Ax_trackBar.Value.ToString("x2");
            }
            else
            {
                RM692AX_Reg_Data.Text = "51" + "," + RM692Ax_trackBar.Value.ToString("x4").Substring(0, 2) + "," + RM692Ax_trackBar.Value.ToString("x4").Substring(2, 2);
            }
            RM692AX_Others_Click(RM692Ax_RegW, e);
        }

        private void RM692Ax_Gamma_Load(object sender, EventArgs e)
        {
            string[] s = { ".bmp" };
            this.ImageType = new List<string>(s);
        }

        private void T20_Tick(object sender, EventArgs e)
        {
            RM692Ax_AUTO_gamma(null, null);
        }

        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {

                case "btnPatternW":                             //写入Pattern参数
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;
                case "btnPrevPic": //Send_buf[1] = 0x1B; break; //上一张图片
                    {
                        Send_buf[1] = 0x1b;
                    }
                    break;
                case "btnNextPic": //Send_buf[1] = 0x1C; break; //下一张图片
                    {
                        Send_buf[1] = 0x1c;
                    }
                    break;
                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //R
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break; //G
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break; //B
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break; //W
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //BL


                case "btnPattern16":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 16;
                        Send_buf[4] = 16;
                        Send_buf[5] = 16;
                    }
                    break; //16灰阶

                case "btnPattern32":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 32;
                        Send_buf[4] = 32;
                        Send_buf[5] = 32;
                    }
                    break; //32灰阶

                case "btnPattern64":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 64;
                        Send_buf[4] = 64;
                        Send_buf[5] = 64;
                    }
                    break; //64灰阶

                case "btnPattern96":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 96;
                        Send_buf[4] = 96;
                        Send_buf[5] = 96;
                    }
                    break; //96灰阶

                case "btnPattern128":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 128;
                        Send_buf[4] = 128;
                        Send_buf[5] = 128;
                    }
                    break; //128灰阶
                case "btnPattern192":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 192;
                        Send_buf[4] = 192;
                        Send_buf[5] = 192;
                    }
                    break; //192灰阶

                case "btnPattern224":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 224;
                        Send_buf[4] = 224;
                        Send_buf[5] = 224;
                    }
                    break; //224灰阶
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 25);
                return;
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }

        private void btnCleantextbox1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
        //浏览图片
        string defaultfilePath = "";
        string path = "";
        private void BtnBrowsePic_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnBrowsePic, "右键点此按钮刷新当前目录下图片");
        }
        private List<string> ImageType;
        private void BtnBrowsePic_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (!Directory.Exists(path))
                {
                    MessageBox.Show("图片文件夹不存在.");
                    return;
                }
                DirectoryInfo di = new DirectoryInfo(path);
                this.listBox3_BMPFile.Items.Clear();
                this.CreateListItem(di);//递归加载文件夹里的所有图片
                textBox1.Text += "图片文件夹已刷新\r\n";
            }
        }
        private void btnBrowsePic_Click(object sender, EventArgs e)    //载入画面按键  选择文件夹
        {
            if (textBox2.Text != "")
            {
                defaultfilePath = textBox2.Text;
            }
            else
            {
                StreamReader streamReader = new StreamReader("./update/Picaddr.txt", Encoding.Default);
                defaultfilePath = streamReader.ReadLine();
                streamReader.Close();
            }
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            //首次defaultfilePath为空，按FolderBrowserDialog默认设置（即桌面）选择
            if (defaultfilePath != "")
            {                //设置此次默认目录为上一次选中目录
                fldb.SelectedPath = defaultfilePath;
            }
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            defaultfilePath = fldb.SelectedPath;//记录选中的目录
            path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo di = new DirectoryInfo(path);
            this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(di);//递归加载文件夹里的所有图片
            textBox2.Text = path;
            // -------------------------------------------------------------------------
            StreamWriter sw = null; //文件流
            sw = new StreamWriter("./update/Picaddr.txt", false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
            sw.Write(defaultfilePath);
            sw.Dispose(); //释放资源
            sw.Close(); //关闭数据流

        }
        /// <summary>
        /// 递归文件夹，把文件夹里所有后缀类似图片的文件，加入listbox
        /// </summary>
        /// <param name="di"></param>
        private void CreateListItem(DirectoryInfo di)
        {
            foreach (DirectoryInfo childDi in di.GetDirectories())
            {
                this.CreateListItem(childDi);
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                if (ImageType.Contains(fi.Extension))//找到一个图片(如果用户手贱把一个excel文件后缀改成jpeg没办法)
                {
                    this.listBox3_BMPFile.Items.Add(new MyImage(fi.FullName));
                }
            }
        }
        private void lstImage_Click(object sender, EventArgs e)
        {
            if (listBox3_BMPFile.SelectedItem == null)
            {
                return;
            }
            MyImage selectedImg = (MyImage)listBox3_BMPFile.SelectedItem;
            //Image img = new Bitmap(selectedImg.Path);
            //pictureBox1.Image = img;
            FileStream fileStream = new FileStream(selectedImg.Path, FileMode.Open, FileAccess.Read);
            pictureBox1.Image = Image.FromStream(fileStream);
            fileStream.Close();
            fileStream.Dispose();
        }
        System.DateTime TimeNow2 = new DateTime();
        TimeSpan TimeCount2 = new TimeSpan();
        private void btnPrevPic_Click(object sender, EventArgs e)
        {
            FileStream aFile;
            Int32 m_txlen = 0;

            int HACT_True;
            int VACT_True;
            int j;
            TimeNow2 = DateTime.Now; //开始计时

            byte[] TemFileContent = new byte[512 * 4000 + 100];
            int ByteReaded = 0x00;

            string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();

            aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);
            if (frmHelp.HACT.Value != HACT_True)
            {
                textBox1.Text += "图片宽度与设定不符\r\n";
                return;
            }

            if ((frmHelp.Dis_mode.SelectedIndex == 1)) //(Dis_mode.SelectedIndex == 1):MIPI 模式，因为下位机仅对MIPI模式做了优化
            {
                //------------20180903---------------
                try
                {   //bd 5a a5 9DF0-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    //BD A5 5A 9DF0-----提前下个标志位告诉下位机开始 快速 送图了
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0xa5; TemFileContent[2] = 0x5a; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    frmHelp.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 0;
                    int x, y, s = 0;
                    x = 512 * 4000; //单次传输数量
                    y = (VACT_True * HACT_True * 3 / x); //传输次数
                    if ((VACT_True * HACT_True * 3 % x) != 0)
                        y++;
                    {
                        //aFile.Seek(54, SeekOrigin.Begin);
                        while (j < y)
                        {
                            //if (HACT_True % 4 != 0)
                            //    aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            //else
                            //    aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                            aFile.Seek((j) * x + 54, SeekOrigin.Begin);

                            ByteReaded = aFile.Read(TemFileContent, 0, x);

                            try
                            {
                                frmHelp.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }

                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            frmHelp.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }
            else
            {
                //------------20180808---------------
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    frmHelp.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 1;
                    //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                    {
                        aFile.Seek(54, SeekOrigin.Begin);
                        while (j <= VACT_True)
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);


                            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 

                            try
                            {
                                frmHelp.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }
                            m_txlen += ByteReaded;
                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            frmHelp.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            return;
                        }

                        aFile.Close();
                    }
                    /*
                    else
                    {
                        MessageBox.Show("BMP分辨率非法");
                    }*/
                }
                catch
                {
                    textBox1.Text += "啥图片都没显示";
                    textBox1.Select(textBox1.Text.Length, 0);
                    textBox1.ScrollToCaret();
                }
            }


            TimeCount2 = DateTime.Now - TimeNow2;
            textBox1.Text = string.Format("{0:00}:{1:00}", TimeCount2.Seconds, TimeCount2.Milliseconds);
        }
        public class MyImage
        {
            public MyImage(string p)
            {
                this.Path = p;
            }
            public string Path { get; set; }
            public override string ToString()
            {
                if (Path == null)
                {
                    return null;
                }
                return System.IO.Path.GetFileName(this.Path);
            }
        }

        private void BtnPreLoadBMP_Click(object sender, EventArgs e)
        {
            DirectoryInfo folder = new DirectoryInfo(textBox2.Text);

            foreach (FileInfo file in folder.GetFiles("*.bmp"))
            {

                Console.WriteLine(file.FullName);
                textBox1.Text += (file.FullName) + "\r\n";


                Int32 HACT_True = 0;
                Int32 VACT_True = 0;
                Int32 m_txlen = 0;
                Int32 j = 0;
                TimeNow2 = DateTime.Now; //开始计时

                byte[] TemFileContent = new byte[4096 * 3];
                System.Int32 ByteReaded = 0x00;
                try
                {
                    string FileName = (file.FullName);//textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();

                    FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    ByteReaded = aFile.Read(TemFileContent, 0, 54);

                    HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                    VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                    if (frmHelp.HACT.Value != HACT_True)
                    {
                        textBox1.Text += "图片宽度与设定不符\r\n";
                        goto startshow;
                    }
                    j = 1;
                    {
                        aFile.Seek(54, SeekOrigin.Begin);
                        while (j <= VACT_True)
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);


                            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3);

                            try
                            {
                                frmHelp.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }
                            m_txlen += ByteReaded;
                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";

                            Application.DoEvents();
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            frmHelp.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                    /*
                    else
                    {
                        MessageBox.Show("BMP分辨率非法");
                    }*/
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            startshow: Thread.Sleep(100);
                TimeCount2 = DateTime.Now - TimeNow2;
                textBox1.Text = string.Format("{0:00}:{1:00}", TimeCount2.Seconds, TimeCount2.Milliseconds);

            }
        }

        private void Btn_load_gamma_patten_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            Gamma_loading_path.Text = fldb.SelectedPath;

            if (!Directory.Exists(Gamma_loading_path.Text))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
        }

        private void RM692Ax_AUTO_gamma_stop(object sender, EventArgs e)
        {
            T20.Enabled = false;
        }
        private void RM692Ax_Inter_Gamma1_Click(object sender, EventArgs e)
        {
            RM692AX_Reg_Data.Text = "fe,70";
            RM692AX_Others_Click(RM692Ax_RegW, null);
            Thread.Sleep(20);
            RM692AX_Reg_Data.Text = "71,00";
            RM692AX_Others_Click(RM692Ax_RegW, null);
            Thread.Sleep(20);
            RM692AX_Reg_Data.Text = "72,31";
            RM692AX_Others_Click(RM692Ax_RegW, null);
        }
        private void RM692Ax_Exit_Gamma1_Click(object sender, EventArgs e)
        {
            RM692AX_Reg_Data.Text = "fe,70";
            RM692AX_Others_Click(RM692Ax_RegW, null);
            Thread.Sleep(20);
            RM692AX_Reg_Data.Text = "71,01";
            RM692AX_Others_Click(RM692Ax_RegW, null);
            Thread.Sleep(20);
            RM692AX_Reg_Data.Text = "72,30";
            RM692AX_Others_Click(RM692Ax_RegW, null);
        }

    }
}
